package wigiri.team.info.copy;

import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.StringSelection;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.eclipse.core.resources.mapping.ModelProvider;
import org.eclipse.core.resources.mapping.ResourceMapping;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.core.runtime.Platform;
import org.eclipse.jface.action.Action;
import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.StructuredViewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.KeyAdapter;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.team.ui.mapping.ISynchronizationCompareAdapter;
import org.eclipse.ui.IContributorResourceAdapter;
import org.eclipse.ui.ide.IContributorResourceAdapter2;

import org.eclipse.ui.navigator.CommonActionProvider;


import wigiri.team.info.Activator;

public class CopyAction extends Action {
	private CommonActionProvider provider;
	public CopyAction(CommonActionProvider provider, StructuredViewer viewer, String label){
		super(label);
		this.provider = provider;
		this.setActionDefinitionId("wigiri.team.info.commands.CopyAction");
		this.hookKeyboardActions(viewer);
//		System.out.println("CopyAction constructor run");
	}
	
	public void run() {
//		System.out.println("run!");
		SimpleDateFormat df = new SimpleDateFormat("HH:mm");
		String timeStamp = df.format(new Date()) + "\n";
		
		IPreferenceStore store = Activator.getDefault().getPreferenceStore();
		String prefix = store.getString("PREFIX_PATH");
		

		StringBuffer buffer = new StringBuffer();
		buffer.append(timeStamp);
		boolean newLine = false;
				
		IStructuredSelection selection = (IStructuredSelection)this.provider.getContext().getSelection();
		ResourceMapping[] mappings = getResourceMappings(selection.toArray());
		for (int i = 0; i < mappings.length; i++) {
			ResourceMapping mapping = mappings[i];
			ISynchronizationCompareAdapter adapter = getCompareAdpater(mapping);
			if (adapter != null) {
				String location = adapter.getPathString(mapping);
				if (newLine) {
					buffer.append("\n");
				}
				buffer.append(prefix + "/" + location);
				newLine = true;
			}
		}
		Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
		StringSelection strSel = new StringSelection(buffer.toString());
		clipboard.setContents(strSel, null);
	}
	
	protected ISynchronizationCompareAdapter getCompareAdpater(ResourceMapping mapping) {
		if (mapping != null) {
			ModelProvider provider = mapping.getModelProvider();
			if (provider != null) {
				Object o = provider.getAdapter(ISynchronizationCompareAdapter.class);
				if (o instanceof ISynchronizationCompareAdapter) {
					return (ISynchronizationCompareAdapter) o;
				}
			}
		}
		return null;
	}
	
	private ResourceMapping[] getResourceMappings(Object[] objects) {
		List <ResourceMapping>result = new ArrayList<ResourceMapping>();
		for (int i = 0; i < objects.length; i++) {
			Object object = objects[i];
			ResourceMapping mapping = getResourceMapping(object);
			if (mapping != null)
				result.add(mapping);
		}
		return (ResourceMapping[]) result.toArray(new ResourceMapping[result.size()]);
	}
	
	private ResourceMapping getResourceMapping(Object o) {
		if (o instanceof ResourceMapping) {
			return (ResourceMapping) o;
		}
		if (o instanceof IAdaptable) {
			IAdaptable adaptable = (IAdaptable) o;
			Object adapted = adaptable.getAdapter(ResourceMapping.class);
			if (adapted instanceof ResourceMapping) {
				return(ResourceMapping) adapted;
			}
			adapted = adaptable.getAdapter(IContributorResourceAdapter.class);
			if (adapted instanceof IContributorResourceAdapter2) {
				IContributorResourceAdapter2 cra = (IContributorResourceAdapter2) adapted;
				return cra.getAdaptedResourceMapping(adaptable);
			}
		} else {
			Object adapted = Platform.getAdapterManager().getAdapter(o, ResourceMapping.class);
			if (adapted instanceof ResourceMapping) {
				return(ResourceMapping) adapted;
			}
		}
		return null;
	}
	
	private void hookKeyboardActions(StructuredViewer viewer){
		viewer.getControl().addKeyListener(new KeyAdapter(){
//			public void keyReleased(KeyEvent event){
//				handleKeyReleased(event);
//			}
			public void keyPressed(KeyEvent event){
				handleKeyReleased(event);
			}
		});
	}
	
	protected void handleKeyReleased(KeyEvent event){
		boolean ctrlReleased = ((event.stateMask & SWT.CONTROL) != 0);
//		System.out.println(event.keyCode);
		if(ctrlReleased && event.keyCode==100){
			run();		
		}
	}
}
